﻿using Furion.RemoteRequest;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinMessageCustomSendRequest.Types;

namespace Admin.NET.Application.AGV;

//BMS接口
[Client("HrBms")]
public interface IHrBmsApi : IHttpDispatchProxy
{
    // 全局拦截，类中每一个方法都会触发
    [Interceptor(InterceptorTypes.Request)]
    static void OnRequesting(HttpClient client, HttpRequestMessage req)
    {
        req.AppendHeaders(new
        {
            Authorization = GenerateWSSEHeader("admin", "password")
        });
    }

    // 全局拦截，类中每一个方法都会触发
    [Interceptor(InterceptorTypes.Response)]
    static void OnResponsing(HttpClient client, HttpResponseMessage res)
    {

    }

    // 全局拦截，类中每一个方法都会触发
    [Interceptor(InterceptorTypes.Exception)]
    static void OnException(HttpClient client, HttpResponseMessage res, string errors)
    {

    }

    /// <summary>
    ///  获取报警信息
    /// </summary>
    /// <returns></returns>
    [Post("/bms/alarm/getAlarmMessageList", ContentType = "application/json", Encoding = "UTF-8")]
    Task<DHResult<AlarmMessageListDto>> getAlarmMessageList([Body] GetAlarmMessageListDto input);



    /// <summary>
    ///  获取信息
    /// </summary>
    /// <returns></returns>
    [Post("/bms/alarm/getMessage", ContentType = "application/json", Encoding = "UTF-8")]
    Task<DHResult<object>> getMessage([Body] GetAlarmDetailtDto input);



    public static string GenerateWSSEHeader(string username, string password)
    {
        // 获取时间戳，单位为秒
        long timestamp = Convert.ToInt64(DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);

        // 获取“时间戳+密码”的 Str 字符串
        string str = $"{timestamp}{password}";

        // 将 Str 进行 SHA-1 加密后的字符串再转 Base64 进行 encode 编码得到一个 digest 令牌
        string digest = Base64SHA1(str);

        // 组装一个 WSSE 字符串
        string wsseHeader = $"{username}:{timestamp}:{digest}";
        return wsseHeader;
    }

    private static string Base64SHA1(string str)
    {
        var hash = SHA1.Create();
        byte[] inputBytes = Encoding.UTF8.GetBytes(str);
        byte[] hashBytes = hash.ComputeHash(inputBytes);
        return Convert.ToBase64String(hashBytes);
    }
}

